﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LEV_ST_OneWireDataResolution
{
    public partial class LEV_ST_G2_OneWireDataResolution
    {

        const string STR_OutOfRangValue = "None";
        const string STR_float = "float";
        const string STR_char = "char";
        const string STR_uchar = "uchar";
        const string STR_int = "int";
        const string STR_uint = "uint";
        const string STR_long = "long";
        const string STR_ulong = "ulong";
        const string STR_strLen = "str_len";
        const string STR_str = "str";
        const string STR_var = "Var";
        const string STR_offset = "offset";

        public enum ItemNameOfSystemStatus
        {
            //the enum values set as array index
            // 1st item is only for System Data gruop
            // 順序勿改動，enum排列順序會當作Array的index
            SystemDataLength,
            //
            SysModeStatusCode,
            Module_Status,
            Module_Function_Status,
            Add_Module_Function_Status,
            Device_Interface_Status,
            Extend_Device_Interface_Status,
            Capacity_Var_Bits_Status,
            DSG_Current_ADC,
            CHG_Current_ADC,
            VBAT_ADC,
            NTC1_ADC,
            NTC2_ADC,
            Vref_mVoltage,
            Current_Capacity,
            Hold_Capacity,
            AVG_DSG_Current_ADC,
            RECORD_DATA_COUNT,
            MAX_DSG_C_ADC_RECORD,
            MAX_CHG_C_ADC_RECORD,
            MAX_VBAT_ADC_RECORD,
            MIN_VBAT_ADC_RECORD,
            MAX_VBAT_SocLo_ADC_RECORD,
            MAX_VBAT_SocHi_ADC_RECORD,
            DSG_MAX_TH1_ADC_RECORD,
            DSG_MIN_TH1_ADC_RECORD,
            DSG_MAX_TH2_ADC_RECORD,
            DSG_MIN_TH2_ADC_RECORD,
            CHG_Cycle_Count_RECORD,
            CHG_ADC_AccumulatingQ_RECORD_Hi,
            CHG_ADC_AccumulatingQ_RECORD_Lo,
            DSG_ADC_AccumulatingQ_RECORD_Hi,
            DSG_ADC_AccumulatingQ_RECORD_Lo,
            DSG_Cycle_Count_RECORD,
            CHG_MAX_TH1_ADC_RECORD,
            CHG_MIN_TH1_ADC_RECORD,
            CHG_MAX_TH2_ADC_RECORD,
            CHG_MIN_TH2_ADC_RECORD,
            OVP_TIMES,
            UVP_TIMES,
            COCP_TIMES,
            DOCP_TIMES,
            TH1_CHG_UTP_TIMES,
            TH2_CHG_UTP_TIMES,
            CHG_TH1_OTP_TIMES,
            CHG_TH2_OTP_TIMES,
            DSG_TH1_LOW_CURRENT_OTP_TIMES,
            DSG_TH1_HIGH_CURRENT_OTP_TIMES,
            DSG_TH2_LOW_CURRENT_OTP_TIMES,
            DSG_TH2_HIGH_CURRENT_OTP_TIMES,
            RECORD_2nd_DATA_COUNT,
            REAL_FCC_UPDATE_TIMES,
            Real_FCC_In_CoulombCounter,
            Derating_FCC_In_CoulombCounter,
            RM_In_CoulombCounter,
            Capacity_In_CoulombCounter,
            STATIC_OVER_VOLTAGE_HOURS_TIMES,
            CHG_OVER_VOLTAGE_MINUTES_TIMES,
            OVER_LOADING_MINUTES_TIMES,
            FASTER_CHARGING_MINUTES_TIMES,
            CHARGING_IN_HIGH_TEMP_MINUTES_TIMES,
            CHARGING_IN_LOW_TEMP_MINUTES_TIMES,
            STORE_IN_LOW_TEMP1_HOURS_TIMES,
            STORE_IN_LOW_TEMP2_HOURS_TIMES,
            STORE_IN_LOW_TEMP3_HOURS_TIMES,
            STORE_IN_LOW_TEMP4_HOURS_TIMES,
            STORE_IN_HIGH_TEMP1_MINUTES_TIMES,
            STORE_IN_HIGH_TEMP2_MINUTES_TIMES,
            STORE_IN_HIGH_TEMP3_MINUTES_TIMES,
            STORE_IN_HIGH_TEMP4_MINUTES_TIMES,
            STORE_IN_HIGH_TEMP5_MINUTES_TIMES,
            RECORD_3rd_TRACKING_DATA_COUNT,
            NTC1_Real_0p1_KelvinDegree,
            NTC2_Real_0p1_KelvinDegree,
            Last_FCC_Updated_Values,
            AVG_Battery_Voltage_ADC,
            System_Activated_TotalHours,
            System_Device_Extend_1_Status,
            System_Device_Extend_2_Status,
            System_Device_Extend_3_Status,
            Current_ChargingTimes_Minutes,
            Last_ChargingTimes_Minutes,
            Max_ChargingTimes_Minutes,
            Current_InSleepingMode_Times_Hi,
            Current_InSleepingMode_Times_Lo,
            NowTotalTimes_Without_Charging_Minutes_Hi,
            NowTotalTimes_Without_Charging_Minutes_Lo,
            LastTotalTimes_Without_Charging_Minutes_Hi,
            LastTotalTimes_Without_Charging_Minutes_Lo,
            MaxTotalTimes_Without_Charging_Minutes_Hi,
            MaxTotalTimes_Without_Charging_Minutes_Lo,
            NowTotalTimes_Without_DisCharging_Minutes_Hi,
            NowTotalTimes_Without_DisCharging_Minutes_Lo,
            LastTotalTimes_Without_DisCharging_Minutes_Hi,
            LastTotalTimes_Without_DisCharging_Minutes_Lo,
            MaxTotalTimes_Without_DisCharging_Minutes_Hi,
            MaxTotalTimes_Without_DisCharging_Minutes_Lo,
            RECORDING_ADC_DSG_OVER_CURRENT_1_6sec_Hi,
            RECORDING_ADC_DSG_OVER_CURRENT_1_6sec_Lo,
            RECORDING_ADC_DSG_OVER_CURRENT_2_6sec_Hi,
            RECORDING_ADC_DSG_OVER_CURRENT_2_6sec_Lo,
            RECORDING_ADC_DSG_OVER_CURRENT_3_6sec_Hi,
            RECORDING_ADC_DSG_OVER_CURRENT_3_6sec_Lo,
            RECORDING_ADC_DSG_OVER_CURRENT_4_6sec_Hi,
            RECORDING_ADC_DSG_OVER_CURRENT_4_6sec_Lo,
            RECORDING_ADC_DSG_OVER_CURRENT_5_6sec_Hi,
            RECORDING_ADC_DSG_OVER_CURRENT_5_6sec_Lo,
            RECORDING_ADC_DSG_OVER_CURRENT_6_6sec_Hi,
            RECORDING_ADC_DSG_OVER_CURRENT_6_6sec_Lo
        }
        public enum ItemNameOfEepromData
        {
            //the enum values set as array index
            // EEprom Item Name 格式: (必要之格式)
            // 名稱_Var_資料型態_offset_偏移位址
            // 順序勿改動，enum排列順序會當作Array的index
            CHG_mA_To_ADC_Factor_Var_float_offset_0,
            DSG_mA_To_ADC_Factor_Var_float_offset_4,
            VBAT_mV_To_ADC_Factor_Var_float_offset_8,
            Thermistor_mV_To_ADC_Factor_Var_float_offset_12,
            DSG_OP_ADC_OFFSET_Var_char_offset_16,
            CHG_OP_ADC_OFFSET_Var_char_offset_17,
            VBAT_ADC_OFFSET_Var_char_offset_18,
            NTC1_ADC_OFFSET_Var_char_offset_19,
            NTC2_ADC_OFFSET_Var_char_offset_20,
            SOC_ADC_OFFSET_Var_char_offset_21,
            R_Sense_10u_ohm_Var_uint_offset_22,
            MCU_Major_Type_Code_Var_uchar_offset_24,
            MCU_Minor_Type_Code_Var_uchar_offset_25,
            RESERVED_CAL_FOR_OFFSET2_Var_ulong_offset_26,
            RESERVED_CAL_FOR_OFFSET3_Var_uint_offset_30,
            MAX_DSG_C_ADC_RECORD_EEPROM_Var_uint_offset_32,
            MAX_CHG_C_ADC_RECORD_EEPROM_Var_uint_offset_34,
            MAX_VBAT_ADC_RECORD_EEPROM_Var_uint_offset_36,
            MIN_VBAT_ADC_RECORD_EEPROM_Var_uint_offset_38,
            MAX_VBAT_SocLo_ADC_RECORD_EEPROM_Var_uint_offset_40,
            MAX_VBAT_SocHi_ADC_RECORD_EEPROM_Var_uint_offset_42,
            DSG_MAX_TH1_ADC_RECORD_EEPROM_Var_uint_offset_44,
            DSG_MIN_TH1_ADC_RECORD_EEPROM_Var_uint_offset_46,
            DSG_MAX_TH2_ADC_RECORD_EEPROM_Var_uint_offset_48,
            DSG_MIN_TH2_ADC_RECORD_EEPROM_Var_uint_offset_50,
            RECORD_DATA_COUNT_EEPROM_Var_uint_offset_52,
            CHG_Cycle_Count_RECORD_EEPROM_Var_uint_offset_54,
            CHG_ADC_AccumulatingQ_RECORD_EEPROM_Var_ulong_offset_56,
            DSG_ADC_AccumulatingQ_RECORD_EEPROM_Var_ulong_offset_60,
            DSG_Cycle_Count_RECORD_EEPROM_Var_uint_offset_64,
            RESERVED_Recording_FOR_OFFSET1_Var_uint_offset_66,
            CHG_MAX_TH1_ADC_RECORD_EEPROM_Var_uint_offset_68,
            CHG_MIN_TH1_ADC_RECORD_EEPROM_Var_uint_offset_70,
            CHG_MAX_TH2_ADC_RECORD_EEPROM_Var_uint_offset_72,
            CHG_MIN_TH2_ADC_RECORD_EEPROM_Var_uint_offset_74,
            G_OVP_TIMES_RECORD_EEPROM_Var_uint_offset_76,
            G_UVP_TIMES_RECORD_EEPROM_Var_uint_offset_78,
            G_COCP_TIMES_RECORD_EEPROM_Var_uint_offset_80,
            G_DOCP_TIMES_RECORD_EEPROM_Var_uint_offset_82,
            G_TH1_CHG_UTP_TIMES_RECORD_EEPROM_Var_uint_offset_84,
            G_TH2_CHG_UTP_TIMES_RECORD_EEPROM_Var_uint_offset_86,
            G_CHG_TH1_OTP_TIMES_RECORD_EEPROM_Var_uint_offset_88,
            G_CHG_TH2_OTP_TIMES_RECORD_EEPROM_Var_uint_offset_90,
            RECORD_2nd_DATA_COUNT_EEPROM_Var_uint_offset_92,
            RESERVED_Recording_FOR_OFFSET3_Var_uint_offset_94,
            SystemSetting_Info_Byte1_Var_uchar_offset_96,
            SystemSetting_Info_Byte2_Var_uchar_offset_97,
            System_Control_Bit_EEPROM_Var_uchar_offset_98,
            SystemSetting_Info_Byte3_Var_uchar_offset_99,
            VERSION_Var_uchar_offset_100,
            MINOR_VERSION_Var_uchar_offset_101,
            EEPROM_VERSION_Var_uchar_offset_102,
            RESERVED_VERSION_Var_uchar_offset_103,
            NUMBER_OF_PARALLEL_CELLS_Var_uchar_offset_104,
            NUMBER_OF_SERIES_CELLS_Var_uchar_offset_105,
            MANUFACTURE_DATE_Var_uint_offset_106,
            SERIAL_NUMBER_Var_uint_offset_108,
            CELL_TYPE_LENGTH_Var_str_len_offset_110,
            CELL_TYPE_Var_str_offset_111,
            MANUFACTURE_NAME_LENGTH_Var_str_len_offset_119,
            MANUFACTURE_NAME_Var_str_offset_120,
            ADC_CURRENT_DETECT_FOR_DSG_STATUS_Var_uint_offset_128,
            ADC_CURRENT_DETECT_FOR_CHG_STATUS_Var_uint_offset_130,
            ADC_DOC_PROTECTION_Var_uint_offset_132,
            ADC_COC_PROTECTION_Var_uint_offset_134,
            ADC_DSG_HIGH_CURRENT_DETECT_Var_uint_offset_136,
            ADC_CHG_CHARGER_TAPE_CURRENT_Var_uint_offset_138,
            ADC_AccQ_FOR_CHG_TH_Var_ulong_offset_140,
            Cycle_Count_FOR_CHG_1st_TH_Var_uint_offset_144,
            Cycle_Count_FOR_CHG_2nd_TH_Var_uint_offset_146,
            ADC_BATTERY_OV_PROTECTION_Var_uint_offset_148,
            ADC_BATTERY_OV_RELEASE_Var_uint_offset_150,
            ADC_BATTERY_UV_PROTECTION_Var_uint_offset_152,
            ADC_BATTERY_UV_RELEASE_Var_uint_offset_154,
            ADC_DSG_OT_HIGH_PROTECTION_Var_uint_offset_156,
            ADC_DSG_OT_HIGH_RELEASE_Var_uint_offset_158,
            ADC_DSG_OT_LOW_PROTECTION_Var_uint_offset_160,
            ADC_DSG_OT_LOW_RELEASE_Var_uint_offset_162,
            ADC_CHG_OT_PROTECTION_Var_uint_offset_164,
            ADC_CHG_OT_RELEASE_Var_uint_offset_166,
            ADC_UT_PROTECTION_Var_uint_offset_168,
            ADC_UT_RELEASE_Var_uint_offset_170,
            ADC_INITIAL_CHARGING_TEMP_RANGE_HI_Var_uint_offset_172,
            ADC_INITIAL_CHARGING_TEMP_RANGE_LO_Var_uint_offset_174,
            ADC_LOW_TEMP_SOC_CHARGING_RANGE_HI_Var_uint_offset_176,
            ADC_LOW_TEMP_SOC_CHARGING_RANGE_LO_Var_uint_offset_178,
            ADC_AccQ_FOR_DSG_TH_Var_ulong_offset_180,
            DSG_CAPACITY_DISPLAY_TH_1_Var_uint_offset_184,
            DSG_CAPACITY_DISPLAY_TH_2_Var_uint_offset_186,
            DSG_CAPACITY_DISPLAY_TH_3_Var_uint_offset_188,
            DSG_CAPACITY_DISPLAY_TH_4_Var_uint_offset_190,
            DSG_CAPACITY_DISPLAY_TH_5_Var_uint_offset_192,
            CHG_CAPACITY_DISPLAY_TH_1_Var_uint_offset_194,
            CHG_CAPACITY_DISPLAY_TH_2_Var_uint_offset_196,
            CHG_CAPACITY_DISPLAY_TH_3_Var_uint_offset_198,
            CHG_CAPACITY_DISPLAY_TH_4_Var_uint_offset_200,
            _ADC_LOOKUP_DSG_TEMP_1_TH__Var_uint_offset_202,
            _ADC_LOOKUP_DSG_TEMP_2_TH__Var_uint_offset_204,
            _ADC_LOOKUP_DSG_TEMP_3_TH__Var_uint_offset_206,
            _ADC_LOOKUP_CHG_TEMP_1_TH__Var_uint_offset_208,
            _ADC_LOOKUP_CHG_TEMP_2_TH__Var_uint_offset_210,
            _ADC_DSG_CURRENT_LOOKUP_OCV_TABLE_TH1__Var_uint_offset_212,
            _ADC_DSG_CURRENT_LOOKUP_OCV_TABLE_TH2__Var_uint_offset_214,
            _ADC_DSG_CURRENT_LOOKUP_OCV_TABLE_TH3__Var_uint_offset_216,
            _ADC_DSG_CURRENT_LOOKUP_OCV_TABLE_TH4__Var_uint_offset_218,
            _ADC_DSG_CURRENT_LOOKUP_OCV_TABLE_TH5__Var_uint_offset_220,
            _ADC_DSG_CURRENT_LOOKUP_OCV_TABLE_TH6__Var_uint_offset_222,
            AUTO_ENTRY_SLEEPING_DELAY_MINUTES_Var_uint_offset_224,
            CAPACITY_DIFFERENCE_VALUES_Var_uint_offset_226,
            PROJECT_NAME_MAX_LENGTH_Var_str_len_offset_228,
            PROJECT_NAME_Var_str_offset_229,
            MCU_UID_MAX_LENGTH_Var_str_len_offset_253,
            MCU_UID_Var_str_offset_254,
            BAR_CODE_REAL_STORE_LENGTH_Var_uchar_offset_266,
            BAR_CODE_MAX_LENGTH_Var_str_len_offset_267,
            BAR_CODE_Var_str_offset_268,
            DESIGN_CAPACITY_mAH_Var_uint_offset_292,
            Last_Record_FCC_mAh_RECORD_EEPROM_Var_uint_offset_294,
            REAL_FCC_UPDATE_TIMES_RECORD_EEPROM_Var_uint_offset_296,
            REAL_FCC_mAH_RECORD_EEPROM_Var_uint_offset_298,
            STATIC_OVER_VOLTAGE_HOURS_TIMES_RECORD_EEPROM_Var_uint_offset_300,
            CHG_OVER_VOLTAGE_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_302,
            OVER_LOADING_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_304,
            FASTER_CHARGING_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_306,
            CHARGING_IN_HIGH_TEMP_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_308,
            CHARGING_IN_LOW_TEMP_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_310,
            STORE_IN_LOW_TEMP1_HOURS_TIMES_RECORD_EEPROM_Var_uint_offset_312,
            STORE_IN_LOW_TEMP2_HOURS_TIMES_RECORD_EEPROM_Var_uint_offset_314,
            STORE_IN_LOW_TEMP3_HOURS_TIMES_RECORD_EEPROM_Var_uint_offset_316,
            STORE_IN_LOW_TEMP4_HOURS_TIMES_RECORD_EEPROM_Var_uint_offset_318,
            G_DSG_TH1_LOW_CURRENT_OTP_TIMES_RECORD_EEPROM_Var_uint_offset_320,
            G_DSG_TH1_HIGH_CURRENT_OTP_TIMES_RECORD_EEPROM_Var_uint_offset_322,
            G_DSG_TH2_LOW_CURRENT_OTP_TIMES_RECORD_EEPROM_Var_uint_offset_324,
            G_DSG_TH2_HIGH_CURRENT_OTP_TIMES_RECORD_EEPROM_Var_uint_offset_326,
            STORE_IN_HIGH_TEMP1_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_328,
            STORE_IN_HIGH_TEMP2_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_330,
            STORE_IN_HIGH_TEMP3_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_332,
            STORE_IN_HIGH_TEMP4_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_334,
            STORE_IN_HIGH_TEMP5_MINUTES_TIMES_RECORD_EEPROM_Var_uint_offset_336,
            RECORD_3rd_TRACKING_DATA_COUNT_EEPROM_Var_uint_offset_338,
            LOW_TEMP1_IN_DSG_ACT_RECORD_MINUTES_EEPROM_Var_uint_offset_340,
            LOW_TEMP2_IN_DSG_ACT_RECORD_MINUTES_EEPROM_Var_uint_offset_342,
            LOW_TEMP3_IN_DSG_ACT_RECORD_MINUTES_EEPROM_Var_uint_offset_344,
            RECORDING_System_Activated_Times_For_TotalHours_Var_int_offset_346,
            RECORDING_Reserved_Data_Var_uchar_offset_348,
            RECORDING_Reserved_Data1_Var_uchar_offset_349,
            RECORDING_Reserved_Data2_Var_uchar_offset_350,
            RECORDING_System_Activated_Times_Minutes_For_1_Hours_Var_uchar_offset_351,
            ShakeSwitchDetect_RangeTime_By_AutoWakeUp_TimeCycles_Var_uchar_offset_352,
            ShakeSwitchTriggerTimes_For_WakeUp_Var_uchar_offset_353,
            GSM_SetInterLockTimes_BeforeBMSSendData_Var_uchar_offset_354,
            GSM_SetDelayTimes_ForBMSCheckMosStatus_Var_uchar_offset_355,
            GSM_System_Enter_SleepingMode_DelayTime_For_STATIC_Var_uint_offset_356,
            GSM_SendDataFor_EachDelayTimes_For_DSG_Var_uint_offset_358,
            GSM_SendDataFor_EachDelayTimes_For_CHG_Var_uint_offset_360,
            GSM_SendData_For_StaticSleeping_By_AutoWakeUp_TimeCycles_Var_uint_offset_362,
            GSM_System_WakeUp_ReadyTimes_In_SleepingMode_Var_uint_offset_364,
            SleepMode_Autowakeup_Intervaltimes_ms_Var_uint_offset_366,
            SleepMode_Autowakeup_Intervaltimes_sec_Var_float_offset_368,
            SleepMode_Autowakeup_Intervaltimes_min_Var_float_offset_372,
            LastTotalTimes_Without_Charging_Minutes_RECORD_EEPROM_Var_ulong_offset_376,
            MaxTotalTimes_Without_Charging_Minutes_RECORD_EEPROM_Var_ulong_offset_380,
            LastTotalTimes_Without_DisCharging_Minutes_RECORD_EEPROM_Var_ulong_offset_384,
            MaxTotalTimes_Without_DisCharging_Minutes_RECORD_EEPROM_Var_ulong_offset_388,
            Last_ChargingTimes_Minutes_RECORD_EEPROM_Var_uint_offset_392,
            Max_ChargingTimes_Minutes_RECORD_EEPROM_Var_uint_offset_394,
            RECORDING_ADC_DSG_OVER_CURRENT_1_6SEC_EEPROM_Var_ulong_offset_396,
            RECORDING_ADC_DSG_OVER_CURRENT_2_6SEC_EEPROM_Var_ulong_offset_400,
            RECORDING_ADC_DSG_OVER_CURRENT_3_6SEC_EEPROM_Var_ulong_offset_404,
            RECORDING_ADC_DSG_OVER_CURRENT_4_6SEC_EEPROM_Var_ulong_offset_408,
            RECORDING_ADC_DSG_OVER_CURRENT_5_6SEC_EEPROM_Var_ulong_offset_412,
            RECORDING_ADC_DSG_OVER_CURRENT_6_6SEC_EEPROM_Var_ulong_offset_416,
            EEPROM_END_DATA_POSITION_Var_uchar_offset_420,
            EEPROM_PN_INFO_START_DATA_POSITION_Var_uchar_offset_421,
            PN_DAK_NAME_MAX_LENGTH_Var_str_len_offset_422,
            PN_DAK_NAME_Var_str_offset_423,
            PN_85K_NAME_MAX_LENGTH_Var_str_len_offset_447,
            PN_85K_NAME_Var_str_offset_448,
            Cell_PN_NAME_MAX_LENGTH_Var_str_len_offset_472,
            Cell_PN_NAME_Var_str_offset_473,
            EEPROM_PN_INFO_END_DATA_POSITION_Var_uchar_offset_497,
        }



        public enum DataGroup
        {
            SystemStatus,
            EepromData
        }
        public enum ItemDataType
        {
            _uchar,
            _char,
            _uint,
            _int,
            _ulong,
            _long,
            _float,
            _string
        }

        public struct DetailOfItem
        {
            public string Name;
            public ItemDataType Type;
            public dynamic Values;
        }

        private struct st_EepromTypeInform
        {
            public ItemNameOfEepromData enumName;
            public string Name;
            public ItemDataType type;
            public int offset;
            public int length;
        }
        private struct st_SystemStatusTypeInform
        {
            public ItemNameOfSystemStatus enumName;
            public string Name;
            public ItemDataType type;
            public int offset;
            public int length;
        }        
        byte[] gRawSystemData;
        byte[] gRawEepromData;


        st_EepromTypeInform[] EepromTypeInform;
        dynamic[] EepromValues;
        st_SystemStatusTypeInform[] SystemStatusTypeInform;
        dynamic[] SystemStatusValues;

        public struct DetailBitFlagOfItemOfSystem
        {
            public BitFlagItemsOfSystem enumName;
            public string Name;
            public ushort bitMask;
            public string groupName;
            public bool flag;
        }
        //private struct st_BitFlagTypeInformOfSystem
        //{
        //    public BitFlagItemsOfSystem enumName;
        //    public string Name;
        //    public ushort bitMask;
        //    public string groupName;
        //}
        DetailBitFlagOfItemOfSystem[] BitFlagTypeInformOfSystem;
        //bool[] BitFlagValueOfSystem;

        public LEV_ST_G2_OneWireDataResolution()
        {
            InitialData();
        }
        public LEV_ST_G2_OneWireDataResolution(byte[] systemData, byte[] eepromData)
        {
            InitialData();
            SetData(DataGroup.SystemStatus, systemData);
            SetData(DataGroup.EepromData, eepromData);
        }
        private void InitialData()
        {
            analyzingEnumItemOfEepormData();
            analyzingEnumItemOfSystemStatus();
            analyzingEnumBitFlagOfSystem();
        }

        private void analyzingEnumItemOfEepormData()
        {
            string[] stArr = Enum.GetNames(typeof(ItemNameOfEepromData));
            EepromTypeInform = new st_EepromTypeInform[stArr.Length];
            
            string str1;
            string[] strA;
            
            int ItemIdx = 0;
            foreach (string s in stArr)
            {
                str1 = s.Replace("_" + STR_var + "_", "#");
                str1 = str1.Replace("_" + STR_offset + "_", "#");
                strA = str1.Split(new char[] { '#' });
                EepromTypeInform[ItemIdx].enumName = (ItemNameOfEepromData)ItemIdx;
                EepromTypeInform[ItemIdx].Name = strA[0];
                EepromTypeInform[ItemIdx].type = getDataType(strA[1]);
                EepromTypeInform[ItemIdx].offset = Convert.ToInt32(strA[2]);
                ItemIdx++;
            }
            // calculate length
            int[] len = new int[stArr.Length];
            for (int i = 1; i < stArr.Length; i++)
            {
                EepromTypeInform[i - 1].length = EepromTypeInform[i].offset - EepromTypeInform[i - 1].offset;
            }
            EepromTypeInform[stArr.Length - 1].length = 1;
        }
        private void analyzingEnumItemOfSystemStatus()
        {
            string[] stArr = Enum.GetNames(typeof(ItemNameOfSystemStatus));
            SystemStatusTypeInform = new st_SystemStatusTypeInform[stArr.Length];
            
            int ItemIdx = 0;
            foreach (string s in stArr)
            {
                SystemStatusTypeInform[ItemIdx].enumName = (ItemNameOfSystemStatus)ItemIdx;
                SystemStatusTypeInform[ItemIdx].Name = ((ItemNameOfSystemStatus)ItemIdx).ToString();
                SystemStatusTypeInform[ItemIdx].type = ItemDataType._uint;
                SystemStatusTypeInform[ItemIdx].offset = ItemIdx * 2;
                SystemStatusTypeInform[ItemIdx].length = 2;
                ItemIdx++;
            }
        }
        private void analyzingEnumBitFlagOfSystem()
        {
            string[] stArr = Enum.GetNames(typeof(BitFlagItemsOfSystem));
            BitFlagTypeInformOfSystem = new DetailBitFlagOfItemOfSystem[stArr.Length];
            
            int ItemIdx = 0;
            string tempName;
            string[] tempArr;
            foreach (string s in stArr)
            {
                BitFlagTypeInformOfSystem[ItemIdx].enumName = (BitFlagItemsOfSystem)ItemIdx;
                tempName = ((BitFlagItemsOfSystem)ItemIdx).ToString();
                tempName = tempName.Replace("_FlagOfBit_", "#");
                tempArr = tempName.Split(new char[] { '#' });
                if(tempArr.Length != 2){
                    throw new ArgumentException("Enum of BitFlagItemsOfSystem format Error");
                }
                BitFlagTypeInformOfSystem[ItemIdx].Name = tempArr[0].TrimEnd(new char[] { '_' });

                tempArr = tempArr[1].Split(new char[] { '_' });
                if ((tempArr.Length < 3) || (!tempArr[1].Equals("Of")))
                {
                    throw new ArgumentException("Enum of BitFlagItemsOfSystem format Error");
                }
                BitFlagTypeInformOfSystem[ItemIdx].bitMask = Convert.ToUInt16(tempArr[0], 16);

                tempName = "";
                for(int i = 3; i < tempArr.Length; i++){
                    tempName += tempArr[i];
                    tempName += "_";
                }
                BitFlagTypeInformOfSystem[ItemIdx].groupName = tempName.TrimEnd(new char[] { '_' });
                ItemIdx++;
            }
        }        

        private ItemDataType getDataType(string str)
        {
            if (str.Equals(STR_float))
            {
                return ItemDataType._float;
            }
            else if (str.Equals(STR_char))
            {
                return ItemDataType._char;
            }
            else if (str.Equals(STR_uchar))
            {
                return ItemDataType._uchar;
            }
            else if (str.Equals(STR_int))
            {
                return ItemDataType._int;
            }
            else if (str.Equals(STR_uint))
            {
                return ItemDataType._uint;
            }
            else if (str.Equals(STR_long))
            {
                return ItemDataType._long;
            }
            else if (str.Equals(STR_ulong))
            {
                return ItemDataType._ulong;
            }
            else if (str.Equals(STR_strLen))
            {
                return ItemDataType._uchar;
            }
            else if (str.Equals(STR_str))
            {
                return ItemDataType._string;
            }
            else
            {
                throw new Exception("DataFormat Error");
            }

        }

        public void SetData(DataGroup type, byte[] data)
        {
            if (type == DataGroup.SystemStatus)
            {
                resolutionSystemStatus(data);
                resolutionBitFlagOfSystem();
            }
            else if (type == DataGroup.EepromData)
            {
                resolutionEEpromData(data);
            }
        }
        private void resolutionSystemStatus(byte[] data)
        {
            gRawSystemData = data;

            int byteArrayLength = gRawSystemData.Length;
            int VarLength = SystemStatusTypeInform.Length;
            SystemStatusValues = new dynamic[VarLength];
            for (int i = 0; i < VarLength; i++)
            {
                if ((SystemStatusTypeInform[i].offset + 2) > byteArrayLength)
                {
                    SystemStatusValues[i] = STR_OutOfRangValue;
                }
                else
                {
                    byte[] newArray = new byte[2];
                    newArray[1] = gRawSystemData[SystemStatusTypeInform[i].offset];
                    newArray[0] = gRawSystemData[SystemStatusTypeInform[i].offset + 1];
                    SystemStatusValues[i] = BitConverter.ToUInt16(newArray, 0);
                }
            }
        }

        private void resolutionEEpromData(byte[] data)
        {
            gRawEepromData = data;

            int byteArrayLength = gRawEepromData.Length;
            int VarLength = EepromTypeInform.Length;
            EepromValues = new dynamic[VarLength];
            for (int i = 0; i < VarLength; i++)
            {
                if (EepromTypeInform[i].type == ItemDataType._float)
                {
                    if ((EepromTypeInform[i].offset + 4) > byteArrayLength)
                    {   //error
                        EepromValues[i] = STR_OutOfRangValue;
                    }
                    else
                    {
                        byte[] newArray = new byte[4];
                        newArray[3] = gRawEepromData[EepromTypeInform[i].offset];
                        newArray[2] = gRawEepromData[EepromTypeInform[i].offset + 1];
                        newArray[1] = gRawEepromData[EepromTypeInform[i].offset + 2];
                        newArray[0] = gRawEepromData[EepromTypeInform[i].offset + 3];
                        EepromValues[i] = BitConverter.ToSingle(newArray, 0);
                    }
                }
                else if (EepromTypeInform[i].type == ItemDataType._string)
                {
                    if ((EepromTypeInform[i].offset + EepromTypeInform[i].length) > byteArrayLength)
                    {   //error
                        EepromValues[i] = STR_OutOfRangValue;
                    }
                    else
                    {
                        byte[] newArray = new byte[EepromTypeInform[i].length];
                        for (int j = 0; j < EepromTypeInform[i].length; j++)
                        {
                            newArray[j] = gRawEepromData[EepromTypeInform[i].offset + j];
                        }
                        EepromValues[i] = LEVUtilityLibrary.MassUtilities.ByteArrayToASCIIChar(newArray);
                    }
                }else{
                    if ((EepromTypeInform[i].offset + EepromTypeInform[i].length) > byteArrayLength)
                    {   //error
                        EepromValues[i] = STR_OutOfRangValue;
                    }
                    else
                    {
                        byte[] newArray = new byte[EepromTypeInform[i].length];
                        int idx = EepromTypeInform[i].length - 1;
                        for (int j = 0; j < EepromTypeInform[i].length; j++)
                        {
                            newArray[idx--] = gRawEepromData[EepromTypeInform[i].offset + j];
                        }
                        if (EepromTypeInform[i].type == ItemDataType._long)
                        {
                            EepromValues[i] = BitConverter.ToInt32(newArray, 0);
                        }
                        else if (EepromTypeInform[i].type == ItemDataType._ulong)
                        {
                            EepromValues[i] = BitConverter.ToUInt32(newArray, 0);
                        }
                        else if (EepromTypeInform[i].type == ItemDataType._int)
                        {
                            EepromValues[i] = BitConverter.ToInt16(newArray, 0);
                        }
                        else if (EepromTypeInform[i].type == ItemDataType._uint)
                        {
                            EepromValues[i] = BitConverter.ToUInt16(newArray, 0);
                        }
                        else if (EepromTypeInform[i].type == ItemDataType._char)
                        {
                            EepromValues[i] = unchecked((sbyte)newArray[0]);
                        }
                        else if (EepromTypeInform[i].type == ItemDataType._uchar)
                        {
                            EepromValues[i] = newArray[0];
                        }
                    }
                }//if-else
            }
        }
        private void resolutionBitFlagOfSystem()
        {
            int VarLength = BitFlagTypeInformOfSystem.Length;
            //BitFlagValueOfSystem = new bool[VarLength];
            for (int i = 0; i < VarLength; i++)
            {
                DetailOfItem di = GetItemInfor<ItemNameOfSystemStatus>(BitFlagTypeInformOfSystem[i].groupName);
                ushort sint = (ushort)di.Values;
                BitFlagTypeInformOfSystem[i].flag = ((sint & BitFlagTypeInformOfSystem[i].bitMask) > 0) ? true : false;
            }
        }
        public bool GetBitFlagOfSystem(BitFlagItemsOfSystem bitItem)
        {
            return BitFlagTypeInformOfSystem[(int)bitItem].flag;
        }
        public DetailBitFlagOfItemOfSystem GetDetailBitFlagOfSystem(BitFlagItemsOfSystem bitItem)
        {
            return BitFlagTypeInformOfSystem[(int)bitItem];
        }
        public DetailOfItem GetItemInfor(Type ItemNameTypeOfGroup, string itemName)
        {
            DetailOfItem item;
            item.Name = "";
            item.Type = ItemDataType._string;
            item.Values = "";
            if (typeof(ItemNameOfEepromData) == ItemNameTypeOfGroup)
            {
                if (EepromValues == null)
                {
                    return item;
                }
                int idx = (int)Enum.Parse(typeof(ItemNameOfEepromData), itemName.ToString());
                item.Name = EepromTypeInform[idx].Name;
                item.Type = EepromTypeInform[idx].type;
                item.Values = EepromValues[idx];
                return item;
            }
            else if (typeof(ItemNameOfSystemStatus) == ItemNameTypeOfGroup)
            {
                if (SystemStatusValues == null)
                {
                    return item;
                }
                int idx = (int)Enum.Parse(typeof(ItemNameOfSystemStatus), itemName.ToString());
                item.Name = SystemStatusTypeInform[idx].Name;
                item.Type = SystemStatusTypeInform[idx].type;
                item.Values = SystemStatusValues[idx];
                return item;
            }
            return item;
        }
        public DetailOfItem GetItemInfor<T>(string itemName) where T : struct, IConvertible
        {
            return GetItemInfor<T>((T)Enum.Parse(typeof(T), itemName));
        }
        public DetailOfItem GetItemInfor<T>(T itemName) where T : struct, IConvertible
        {
            bool flag = false;
            //string str = "";
            if (!typeof(T).IsEnum) throw new ArgumentException("itemName Error, T must be an enumerated type of ItemNameOfSystemStatus or ItemNameOfEepromData");
            if (typeof(ItemNameOfSystemStatus) == typeof(T) ||
                typeof(ItemNameOfEepromData) == typeof(T))
            {
                flag = true;
            }
            if (!flag) throw new ArgumentException("itemName Error, T must be an enumerated type of ItemNameOfSystemStatus or ItemNameOfEepromData");
            DetailOfItem item;
            item.Name = "";
            item.Type = ItemDataType._string;
            item.Values = "";

            if (typeof(ItemNameOfEepromData) == typeof(T))
            {
                if (EepromValues == null)
                {
                    return item;
                }
                int idx = (int)Enum.Parse(typeof(T), itemName.ToString());
                item.Name = EepromTypeInform[idx].Name;
                item.Type = EepromTypeInform[idx].type;
                item.Values = EepromValues[idx];
                return item;
            }
            else if (typeof(ItemNameOfSystemStatus) == typeof(T))
            {
                if (SystemStatusValues == null)
                {
                    return item;
                }
                int idx = (int)Enum.Parse(typeof(T), itemName.ToString());
                item.Name = SystemStatusTypeInform[idx].Name;
                item.Type = SystemStatusTypeInform[idx].type;
                item.Values = SystemStatusValues[idx];
                return item;
            }

            return item;
        }
        public DetailOfItem[] GetAllItemInfor(Type ItemNameTypeOfGroup)
        {
            DetailOfItem[] iteminfo;

            if (typeof(ItemNameOfEepromData) == ItemNameTypeOfGroup)
            {
                if (EepromValues == null)
                {
                    return null;
                }
                iteminfo = new DetailOfItem[EepromTypeInform.Length];
                for (int i = 0; i < EepromTypeInform.Length; i++)
                {
                    iteminfo[i].Name = EepromTypeInform[i].Name;
                    iteminfo[i].Type = EepromTypeInform[i].type;
                    iteminfo[i].Values = EepromValues[i];
                }
                return iteminfo;
            }
            else if (typeof(ItemNameOfSystemStatus) == ItemNameTypeOfGroup)
            {
                if (SystemStatusValues == null)
                {
                    return null;
                }
                iteminfo = new DetailOfItem[SystemStatusTypeInform.Length];
                for (int i = 0; i < SystemStatusTypeInform.Length; i++)
                {
                    iteminfo[i].Name = SystemStatusTypeInform[i].Name;
                    iteminfo[i].Type = SystemStatusTypeInform[i].type;
                    iteminfo[i].Values = SystemStatusValues[i];
                }
                return iteminfo;

            }
            else
            {
                throw new ArgumentException("ItemNameTypeOfGroup Error , ItemNameTypeOfGroup must be an enumerated type (ItemNameOfSystemStatus or ItemNameOfEepromData)");
            }

        }
    }//public class OneWireDataResolution
}//namespace LEV_ST_OneWireDataResolution
